package star.oath.framework.util;

public class CharsetConverter {

    public static void main(String[] args) {
        String original = "侵扰";
        System.out.println("原始字符串: " + original);

        String toIso = gb2312ToIso8859_2(original);
        System.out.println("GB2312 -> ISO8859-2: " + toIso);

        String backToGb = iso8859_2ToGb2312(toIso);
        System.out.println("ISO8859-2 -> GB2312: " + backToGb);
    }

    /**
     * 将GB2312编码的字符串转换为ISO8859-2编码
     * @param gb2312Str GB2312编码的字符串
     * @return ISO8859-2编码的字符串
     */
    public static String gb2312ToIso8859_2(String gb2312Str) {
        try {
            byte[] gb2312Bytes = gb2312Str.getBytes("GB2312");
            return new String(gb2312Bytes, "ISO-8859-2");
        } catch (Exception e) {
            System.err.println("字符集转换出错: " + e.getMessage());
            return null;
        }
    }

    /**
     * 将ISO8859-2编码的字符串转换为GB2312编码
     * @param iso8859_2Str ISO8859-2编码的字符串
     * @return GB2312编码的字符串
     */
    public static String iso8859_2ToGb2312(String iso8859_2Str) {
        try {
            byte[] iso8859_2Bytes = iso8859_2Str.getBytes("ISO-8859-2");
            return new String(iso8859_2Bytes, "GB2312");
        } catch (Exception e) {
            System.err.println("字符集转换出错: " + e.getMessage());
            return null;
        }
    }

}